package com.ayf.payment.game.api.mapper;

import com.ayf.payment.game.api.dto.*;
import com.ayf.payment.game.api.dto.criteria.*;
import com.ayf.payment.game.api.dto.platform.MerchantSettleDTO;
import com.ayf.payment.game.api.entity.Order;
import com.ayf.payment.game.api.type.OrderStatus;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import tk.mybatis.mapper.common.Mapper;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

public interface OrderMapper extends Mapper<Order> {
    /**
     * 查询出处理中的订单信息
     *
     * @param orderStatus
     * @param time
     * @return
     */
    List<OrderAndMerchantDTO> selectOrderAndMerchantInfo(@Param("orderStatus") OrderStatus orderStatus, @Param("time") long time);

    /**
     * 查询出处理中的订单信息
     *
     * @param orderStatus
     * @param merchantOrderNo
     * @return
     */
    OrderAndMerchantDTO getOrderAndMerchantInfo(@Param("orderStatus") OrderStatus orderStatus, @Param("merchantOrderNo") String merchantOrderNo);

    Order selectByMerchantOrderNo(@Param("merchantOrderNo") String merchantOrderNo);

    List<OrderDTO> selectByDTO(OrderSelectCriteria criteria);

    /**
     * 查询出充值的所有账号信息
     *
     * @param ro
     * @return
     */
    List<ReissueOrderDTO> selectRechargeNumbers(@Param("ro") ReissueOrderDTO ro);

    /**
     * 订单信息查询
     *
     * @param cri
     * @return
     */
    List<OrderInfoDTO> selectOrderInfo(@Param("cri") OrderInfoCriteria cri);
    List<OrderInfoDTO> todayLastOrderList(@Param("cri") OrderInfoCriteria cri);

    /**
     * 查询汇总信息
     *
     * @param cri
     * @return
     */
    ChargeOrderResponseDTO selectSumAmountAndFee(@Param("cri") OrderInfoCriteria cri);

    /**
     * 查询推广员订单
     *
     * @param criteria
     * @return
     */
    List<OrderInfoDTO> selectSonOrderList(@Param("cri") OrderInfoCriteria criteria);

    /**
     * 查询推销员汇总信息
     *
     * @param cri
     * @return
     */
    ChargeOrderResponseDTO selectSumSonAmount(@Param("cri") OrderInfoCriteria cri);

    /**
     * 分区统计
     *
     * @param statistics
     * @return
     */
    List<StatisticsDTO> partitionStatistic(@Param("cri") StatisticsCriteria statistics);

    /**
     * 充值类型统计
     *
     * @param statistics
     * @return
     */
    List<StatisticsDTO> payTypeStatistic(@Param("cri") StatisticsNoPageCriteria statistics);

    /**
     * 充值渠道结算统计
     *
     * @param statistics
     * @return
     */
    List<ChannelStatisticsDTO> channelStatistic(@Param("cri") StatisticsCriteria statistics);

    /**
     * 充值模板统计
     *
     * @param statistics
     * @return
     */
    List<StatisticsDTO> templateStatistic(@Param("cri") StatisticsCriteria statistics);

    /**
     * 充值分组统计
     *
     * @param statistics
     * @return
     */
    List<StatisticsDTO> groupStatistic(@Param("cri") StatisticsCriteria statistics);

    /**
     * 充值账号统计
     *
     * @param statistics
     * @return
     */
    List<StatisticsDTO> numberStatistic(@Param("cri") StatisticsCriteria statistics);

    /**
     * 商户充值统计
     *
     * @param statistics
     * @return
     */
    List<StatisticsDTO> merchantStatistic(@Param("cri") StatisticsNoPageCriteria statistics);

    /**
     * 每日充值统计
     *
     * @param statistics
     * @return
     */
    List<StatisticsDTO> dailyStatistic(@Param("cri") StatisticsCriteria statistics);

    /**
     * 时段统计
     *
     * @param statistics
     * @return
     */
    @MapKey("hour")
    Map<Integer, StatisticsDTO> hoursStatistic(@Param("cri") StatisticsNoPageCriteria statistics);

    /**
     * 商户每日充值统计
     *
     * @param statistics
     * @return
     */
    List<StatisticsDTO> merchantDailyStatistic(@Param("cri") StatisticsCriteria statistics);

    /**
     * 代理每日充值统计
     *
     * @param statistics
     * @return
     */
    List<StatisticsDTO> proxyDailyStatistic(@Param("cri") StatisticsCriteria statistics);

    /**
     * 查询商户收入汇总信息
     *
     * @param cri
     * @return
     */
    @MapKey("merchant_id")
    Map<Integer, Map<String, Object>> selectSumAmount(@Param("cri") OrderInfoCriteria cri);

    /**
     * 查询代理商收入汇总信息
     *
     * @param cri
     * @return
     */
    @MapKey("proxy_id")
    Map<Integer, Map<String, Object>> selectSumProxyIncome(@Param("cri") OrderInfoCriteria cri);

    /**
     * 清理订单
     *
     * @param clean
     * @return
     */
    int cleanOrder(@Param("clean") CleanCriteria clean);

    @Update("update g_order set script_status=#{scriptStatus} where merchant_order_no = #{merchantOrderNo}")
    void updateScriptStatus(@Param("merchantOrderNo") String merchantOrderNo, @Param("scriptStatus") int scriptStatus);

    @Update("update g_order set order_status='FAIL',script_status=-1,finish_time=#{finishTime} where order_status = 'PROCESSING' and #{placeTime} > place_order_time and merchant_order_no = #{merchantOrderNo}")
    void updateOrderStatus(@Param("merchantOrderNo") String merchantOrderNo, @Param("placeTime") long placeTime, @Param("finishTime") Date finishTime);

    List<MerchantSettleDTO> merchantSettleByOrderCount(@Param("orderGenerateTimeIndexEnd") Integer orderGenerateTimeIndexEnd, @Param("orderGenerateTimeIndexStrat") Integer orderGenerateTimeIndexStrat, @Param("settleDate") String settleDate, @Param("settleCycle") Short settleCycle);

    List<MerchantSettleDTO> merchantSettleByOrderCountProxy(@Param("orderGenerateTimeIndexEnd") Integer orderGenerateTimeIndexEnd, @Param("orderGenerateTimeIndexStrat") Integer orderGenerateTimeIndexStrat, @Param("settleDate") String settleDate, @Param("settleCycle") Short settleCycle);

    BigDecimal selectPartitionChargeSuccessTotalAmt(@Param("cri") PartitionStatisticsCriteria criteria);

    List<PartitionStatisticsDTO> selectPartitionChargeSuccessDetailList(@Param("cri") PartitionStatisticsCriteria criteria);

    List<Order> getReTryOrder(@Param("merchantOrderNos") String[] merchantOrderNos);

    Order getCorrectOrders(@Param("merchantOrderNo") String merchantOrderNo);

    ChargeOrderResponseDTO partitionStatisticSum(@Param("cri") StatisticsCriteria statistics);

    void deleteOrders(@Param("merchantOrderNos") String[] merchantOrderNos);

    StatisticsPageDTO dailySumStatistic(@Param("cri") StatisticsNoPageCriteria statistics);

    OrderInfoDTO playerQueryOrderNo(@Param("merchantOrderNo")String merchantOrderNo,@Param("orderGenerateTimeIndex")Integer orderGenerateTimeIndex);
}